﻿<?php

/**
 * FormsenderController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Cms_FormsenderController extends Zend_Controller_Action {
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$main = MF_Main::getInstance();
		$form_sender = $main->FormSender->getById($id);
		$view_data = array('email' => $form_sender->email);
		
		
		if (NULL !== $act = $this->getRequest()->getParam('act')) {
			if (NULL === $msg_id = $this->getRequest()->getParam('msg_id')) {
				throw new Zend_Controller_Action_Exception('Required param "Msg Id"', 404);
			}
			
			switch ($act) {
				case 'delete':
					$form_sender->getById($msg_id)->delete();
					break;
				
				case 'opend':
					$form_sender->getById($msg_id)->makeReaded();
					break;
					
				default:
					throw new Zend_Controller_Action_Exception('Act "'.$act.'" not set', 404);;
					break;
			}
			
			
		}
		
		if ($this->_request->isPost()) {
			$post_data = $this->_request->getPost();
			if (!$main->Utils->emailValidator($post_data['email'])) {
				$this->view->errors = array('email' => array('Неверный формат адреса электронной почты'));
				$view_data = $post_data;
			} else {
				$form_sender->email = $post_data['email'];
				
				$this->renderScript('exit.phtml');
				return true;
			}
		}
		
		$this->view->form_id = $form_sender->_content_id;
		$this->view->post_data = $view_data;
		
		
		
		
		$this->view->msg_list = $form_sender->getList();
	}

	
	
}